package org.footballmanager.pandafootball.system.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.domain.FootTeam;
import org.footballmanager.pandafootball.system.domain.FootUser;
import org.footballmanager.pandafootball.system.domain.FootZj;
import org.footballmanager.pandafootball.system.domain.enums.BFLX;
import org.footballmanager.pandafootball.system.form.impl.FootZjSearchForm;
import org.footballmanager.pandafootball.system.mapper.FootZjMapper;
import org.footballmanager.pandafootball.system.service.FootTeamService;
import org.footballmanager.pandafootball.system.service.FootUserService;
import org.footballmanager.pandafootball.system.service.FootZjService;
import org.footballmanager.pandafootball.system.service.FootZnxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class FootZjServiceImpl extends ServiceImpl<FootZj> implements FootZjService {
    private static final double WIN = 10D;
    private static final double SQUARE = 8D;
    private static final double LOSE = 6D;

    private static final double TWIN = 10D;
    private static final double TSQUARE = 8D;
    private static final double TLOSE = 6D;

    private static final double RANKWIN = 3D;
    private static final double RANKSQUARE = 2D;
    private static final double RANKLOSE = 1D;

    @Autowired
    private FootZjMapper footZjMapper;
    @Autowired
    private FootUserService footUserService;
    @Autowired
    private FootTeamService footTeamService;
    @Autowired
    private FootZnxService footZnxService;

    /**
     * 根据用户VIP等级分配所获得的资金
     *
     * @param userId1 用户主键1
     * @param userId2 用户主键2
     * @param type    2代表qd1胜利,1代表平,0代表qd1队负
     * @param isadd   是否增加
     * @param bflx    报分类型
     * @param request 请求
     */
    @Override
    @Transactional
    public void getMoney(Long userId1, Long userId2, int type, boolean isadd, BFLX bflx, HttpServletRequest request) {
        //获取主队和客队的球队信息
        FootTeam zhuteam = footTeamService.getTeamByUserid(userId1);
        FootTeam keteam = footTeamService.getTeamByUserid(userId2);
        FootUser zhuuser = footUserService.get(userId1);
        FootUser keuser = footUserService.get(userId2);
        double zhuVipLevel = footUserService.getVipLevel(zhuuser, request);
        double keVipLevel = footUserService.getVipLevel(keuser, request);
        if (zhuteam != null && keteam != null) {
            //添加资金变动记录
            Double zhuMoney = (type == 0 ? WIN : type == 1 ? SQUARE : LOSE) * zhuVipLevel;
            Double keMoney = (type == 0 ? LOSE : type == 1 ? SQUARE : WIN) * keVipLevel;
            Double zhuTopMoney = (type == 0 ? TLOSE : type == 1 ? TSQUARE : TWIN) * zhuVipLevel;
            Double zhuRankMoney = (type == 0 ? RANKWIN : type == 1 ? RANKSQUARE : RANKLOSE) * zhuVipLevel;
            Double keRankMoney = (type == 0 ? RANKLOSE : type == 1 ? RANKSQUARE : RANKWIN) * keVipLevel;
            if (bflx.equals(BFLX.NORMAL)) {
                this.updateTeamMoney(zhuuser, zhuteam, zhuMoney, isadd, bflx, request);
                this.updateTeamMoney(keuser, keteam, keMoney, isadd, bflx, request);
            } else if (bflx.equals(BFLX.TOP)) {
                this.updateTeamMoney(zhuuser, zhuteam, zhuTopMoney, isadd, bflx, request);
            } else {
                this.updateTeamMoney(zhuuser, zhuteam, zhuRankMoney, isadd, bflx, request);
                this.updateTeamMoney(keuser, keteam, keRankMoney, isadd, bflx, request);
            }
        }
    }

    /**
     * 更新team的资金
     *
     * @param user    用户实体类
     * @param team    team实体类
     * @param money   金额
     * @param isadd   是否是增加
     * @param bflx    报分类型
     * @param request 请求
     */
    private void updateTeamMoney(FootUser user, FootTeam team, double money, boolean isadd, BFLX bflx, HttpServletRequest request) {
        String resultString = isadd ? "获得" : "失去";
        FootZj zj = new FootZj();
        zj.setUserId(user.getId());
        zj.setUserName(user.getNike());
        zj.setBgsj(new Date());
        team.setQdZzj(team.getQdZzj() + (isadd ? money : 0D - money));
        zj.setBgzj(money);
        zj.setBgsm(user.getNike() + resultString + "了" + bflx.getText() + "奖金:" +
                new DecimalFormat(",###").format(money) + "PB,当前球队的总资金为:" +
                new DecimalFormat(",###").format(team.getQdZzj()) + "PB");
        footTeamService.update(team, request);
        this.save(zj, request);
    }

    /**
     * 发送资金变动说明
     *
     * @param userId  用户主键
     * @param jg      要变动的资金数额
     * @param isz     是否是增加资金
     * @param bdsm    变动说明
     * @param request 请求
     */
    @Override
    @Transactional
    public void sendZj(Long userId, Double jg, boolean isz, String bdsm, HttpServletRequest request) {
        FootZj zj = new FootZj();
        if (isz) {
            zj.setBgzj(jg);
        } else {
            zj.setBgzj(0 - jg);
        }
        zj.setBgsj(new Date());
        FootUser user = footUserService.get(userId);
        if (null != user) {
            zj.setUserId(userId);
            zj.setUserName(user.getNike());
        }
        zj.setBgsm(bdsm);
        this.save(zj, request);
    }

    /**
     * 根据所给money发放任务奖励
     *
     * @param userId   用户主键
     * @param money    获得的奖励数
     * @param isReshen 是否是热身赛
     * @param request  请求
     */
    @Override
    public void getMissionMoney(Long userId, Double money, Boolean isReshen, HttpServletRequest request) {
        //任务奖励发放
        String userName = footUserService.get(userId).getNike();
        FootTeam team = footTeamService.getTeamByUserid(userId);
        if (null != team) {
            String result = userName + "完成任务" + (isReshen ? "1" : "2") + ",获得奖励" + money + "PB";
            team.setQdZzj(team.getQdZzj() + money);
            //不要调用updateNotNull逻辑混乱
            footTeamService.update(team, request);
            this.sendZj(userId, money, true, result, request);
            footZnxService.sendZnx(footUserService.getAdminUserid(), userId, result, null, null, request);
        }
    }

    /**
     * 翻页方法
     *
     * @param footZjSearchForm 条件
     * @return 列表
     */
    @Override
    public List<FootZj> findall(FootZjSearchForm footZjSearchForm) {
        return footZjMapper.findall(footZjSearchForm);
    }

    /**
     * 根据主键获取记录
     *
     * @param id 主键
     * @return 记录
     */
    @Override
    public FootZj get(Serializable id) {
        return footZjMapper.findById(id);
    }

    /**
     * 查询
     *
     * @param entityname       实体类名
     * @param map              参数
     * @param footZjSearchForm 条件
     */
    @Override
    public void search(String entityname, Model map, FootZjSearchForm footZjSearchForm) {
        map.addAttribute(String.format(Constants.FORMNAME, entityname), footZjSearchForm);
        Page<FootZj> page = PageHelper.startPage(footZjSearchForm.getPageIndex(), footZjSearchForm.getPageSize());
        List<FootZj> list = this.findall(footZjSearchForm);
        map.addAttribute(Constants.PI, page.toPageInfo());
        map.addAttribute(String.format(Constants.LISTENTITY, entityname), list);
    }

    /**
     * 删除所有
     *
     * @param footZjSearchForm 条件
     * @param request          请求
     */
    @Override
    public void deleteAll(FootZjSearchForm footZjSearchForm, HttpServletRequest request) {
        if (footZjSearchForm.getSelectIds() != null) {
            String[] result = footZjSearchForm.getSelectIds().split(Constants.COMMA);
            for (String id : result) {
                this.delete(Long.parseLong(id), request);
            }
        }
    }

    /**
     * 删除对应用户下的所有记录
     *
     * @param userId  用户主键
     * @param request 请求
     */
    @Override
    public void deleteByUserId(Long userId, HttpServletRequest request) {
        FootZjSearchForm footZjSearchForm = new FootZjSearchForm();
        footZjSearchForm.setUserId(userId);
        List<FootZj> list = this.findall(footZjSearchForm);
        if (list != null && !list.isEmpty()) {
            for (FootZj zj : list) {
                this.delete(zj.getId(), request);
            }
        }
    }
}
